// Methods related to links

import { Meteor } from 'meteor/meteor';

import { Roles } from '/imports/api/roles/roles.js';
// import { check } from 'meteor/check';

Meteor.methods({
  'user.getMobileValid'() {
    var Num="";
	for(var i=0;i<6;i++)
	{
		Num+=Math.floor(Math.random()*10);
	}
	return Num;
  },

  'user.mobile_to_username'(email){
    //change email to mobile
     return Meteor.users.find({profile: {mobile: email}}).fetch()[0];
  },
  'users.delete'(id) {

    return Meteor.users.remove(id);
  },
  'users.block'(id, state) {
    return Meteor.users.update(id, {
            $set: {
              isBanned: state
            }
          });
    
  },
  'member.create'(mobile, username, password){
      let newMemberId = Accounts.createUser({
            username,
            password,
            profile: {
              mobile
            }
          });
      let newMember = Meteor.users.findOne({_id: newMemberId});
      let memberRole = null;
      if (Roles.find({name: "网站成员"}).count() == 0) {
        Roles.insert({
          name: "网站成员",
          weight: 500,
          createdAt: new Date(),
        });
         memberRole = Roles.findOne({name: "网站成员"});
        }else{
           memberRole = Roles.findOne({name: "网站成员"});
          
        }
        Meteor.users.update(admin._id, {
            $set: {
              role_ids: [memberRole._id]
            }
          });
        return newMemberId;
  },
});
